package customer.zspt.cn.customer.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.bean.PersonalIDao;
import customer.zspt.cn.customer.utils.Chinese2SpellUtils;
import customer.zspt.cn.customer.Widget.ContactItemView;


public class ContactsAdapter extends BaseAdapter {
    private BitmapUtils utils;
    private Context context;
    private List<PersonalIDao.userList> name;
    public ContactsAdapter(Context context, List<PersonalIDao.userList> name) {
        utils = new BitmapUtils(context);
        this.name = name;
        this.context = context;
        utils.configDefaultLoadingImage(R.drawable.default_fmessage);
    }
    //判断索引生成的位置
    public int getPositionForSection(char now) {
        if (!(now >= 65 && now <= 90)) {
            now = '#';
        }
        for (int i = 0; i < name.size(); i++) {
            char c = Chinese2SpellUtils.getPingYin(name.get(i).kefuRealname, 3).toUpperCase().charAt(0);
            if (!(c >= 65 && c <= 90)) {
                c = '#';
            }
            if (c == now) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getCount() {
        return name.size();
    }

    @Override
    public String getItem(int position) {
        return name.get(position).kefuRealname;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        View item = View.inflate(context, R.layout.contact_name_item, null);
        TextView titleTx = (TextView) item.findViewById(R.id.indexical_title);
        ContactItemView name_item = (ContactItemView) item.findViewById(R.id.name_item);
        ImageView titleView = name_item.getTitleView();
        utils.display(titleView, name.get(position).kefuAvatar);
        name_item.setText(name.get(position).kefuRealname);
        char indexChar = Chinese2SpellUtils.getPingYin(name.get(position).kefuRealname, 3).toUpperCase().charAt(0);
        if (getPositionForSection(indexChar) == position) {
            titleTx.setText(((indexChar >= 65 && indexChar <= 90) ? indexChar : '#') + "");
        } else {
            titleTx.setVisibility(View.GONE);
        }
        name_item.setText(name.get(position).kefuRealname);
        return item;
    }

}